package com.alex.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alex.entityVo.ArticleSearchVo;
import com.alibaba.fastjson.JSONObject;
import common.beans.ResultBean;
import common.beans.User;
import common.util.UserUtil;
import common.util.YouDaoTransLate;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/ai_writing_assistant")
@RestController
@Tag(name = "核心检索")
public class MainController {

    public static String RESTURL_GPT = "http://10.1.1.11:28080";
    public static String RESTURL_ES = "http://10.1.1.11:29000";

    @Resource
    StaticService staticService;


    @Operation(summary = "检索", description = "第一个筛选, 输入检索词, 返回左侧文章列表")
    @PostMapping(value = "search")
    public String search(@RequestBody ArticleSearchVo articleSearchVo) {
        User user = UserUtil.getUserUnloginDeniey();
        articleSearchVo.setUid(user.getUid());
        articleSearchVo.setGid(user.getGid());
        articleSearchVo.setRealName(user.getRealname());
        staticService.insStat(articleSearchVo.getPram());
        return HttpUtil.post(RESTURL_ES + "/search/search", JSONUtil.toJsonStr(articleSearchVo));
    }

    @Operation(summary = "翻译", description = "")
    @PostMapping(value = "translate")
    public ResultBean<Object> translate(@RequestBody JSONObject jsonObject) {
        String content = jsonObject.getString("content");
        Integer type = jsonObject.getInteger("type");
        if (type == 2) {
            return new ResultBean<>(YouDaoTransLate.DoCnToEnTrans(content));
        }
        return new ResultBean<>(YouDaoTransLate.DoEnToCnTrans(content));
    }


    @Operation(summary = "近似词", description = "语料库检索中使用的近似词")
    @GetMapping(value = "ai_writing_assistant")
    public String ai_writing_assistant(String content) {
        Map<String, Object> map = new HashMap<>();
        map.put("content", content);
        staticService.insStat(content, "近似词");
        return HttpUtil.get(RESTURL_GPT + "/ai_writing_word/ai_writing_word", map);
    }

    @Operation(summary = "语料库检索", description = "第一个筛选, 输入检索词, 返回左侧文章列表")
    @GetMapping(value = "article_search")
    public String article_search(
            @Parameter(name = "param", description = "语料库检索词", example = "Grafts showed a large intimal thickening composed of several layers of smooth muscle cells and an abundant extracellular matrix", required = true) String param,
            @Parameter(name = "page", description = "当前页", example = "1", required = true) Integer page,
            @Parameter(name = "limit", description = "每页显示数量", example = "10", required = true) Integer limit
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("param", param);
        map.put("page", page);
        map.put("limit", limit);
        User user = UserUtil.getUserUnloginDeniey();

        staticService.insStat(param, "语料库检索");
        map.put("uid", user.getUid());
        map.put("gid", user.getGid());
        map.put("realName", user.getRealname());


        return HttpUtil.get(RESTURL_ES + "/ai_writing_assistant/ai_writing_assistant", map);
    }

    @Operation(summary = "语料来源", description = "第一个筛选, 输入检索词, 返回左侧文章列表")
    @GetMapping(value = "article_search_detail")
    public String article_search_detail(
            @Parameter(name = "tableId", description = "tableId", example = "101", required = true) String tableId,
            @Parameter(name = "fileId", description = "fileId", example = "3907", required = true) Integer fileId

    ) {
        Map<String, Object> param = new HashMap<>();
        param.put("tableId", tableId);
        param.put("fileId", fileId);
        param.put("isManual", "false");
        param.put("pmorut", 1);
        String URL = "http://10.1.1.11:8850/web/pub/getpminfoid";
        return HttpUtil.post(URL, param);
    }

}
